var gm = gm || {};

//#region 数字处理
gm.number = gm.number || {};

//对数字进行0补齐处理
gm.number.pad = function (source, length) {
    var pre = "",
        negative = (source < 0),
        string = String(Math.abs(source));

    if (string.length < length) {
        pre = (new Array(length - string.length + 1)).join('0');
    }

    return (negative ? "-" : "") + pre + string;
};
//#endregion


//#region 日期处理
gm.date = gm.date || {};

//对日期对象进行格式化
//source日期对象
//pattern格式化串 
//支持格式 与C# 一致
gm.date.format = function (source, pattern) {
    if ('string' != typeof pattern) {
        return source.toString();
    }

    function replacer(patternPart, result) {
        pattern = pattern.replace(patternPart, result);
    }

    var pad = gm.number.pad,
        year = source.getFullYear(),
        month = source.getMonth() + 1,
        date2 = source.getDate(),
        hours = source.getHours(),
        minutes = source.getMinutes(),
        seconds = source.getSeconds();

    replacer(/yyyy/g, pad(year, 4));
    replacer(/yy/g, pad(parseInt(year.toString().slice(2), 10), 2));
    replacer(/MM/g, pad(month, 2));
    replacer(/M/g, month);
    replacer(/dd/g, pad(date2, 2));
    replacer(/d/g, date2);

    replacer(/HH/g, pad(hours, 2));
    replacer(/H/g, hours);
    replacer(/hh/g, pad(hours % 12, 2));
    replacer(/h/g, hours % 12);
    replacer(/mm/g, pad(minutes, 2));
    replacer(/m/g, minutes);
    replacer(/ss/g, pad(seconds, 2));
    replacer(/s/g, seconds);

    return pattern;
};

//将字符串转化为日期对象
gm.date.parse = function (source) {
    var reg = new RegExp("^\\d+(\\-|\\/)\\d+(\\-|\\/)\\d+\x24");
    if ('string' == typeof source) {
        if (reg.test(source) || isNaN(Date.parse(source))) {
            var d = source.split(/ |T/),
                d1 = d.length > 1
                        ? d[1].split(/[^\d]/)
                        : [0, 0, 0],
                d0 = d[0].split(/[^\d]/);
            return new Date(d0[0] - 0,
                            d0[1] - 1,
                            d0[2] - 0,
                            d1[0] - 0,
                            d1[1] - 0,
                            d1[2] - 0);
        } else {
            return new Date(source);
        }
    }

    return new Date();
};

gm.date.nearstr = function (source) {
    var diff_day = gm.date.date_diff("d", new Date(), source);
    var diff_hour = gm.date.date_diff("h", new Date(), source);
    var diff_minute = gm.date.date_diff("m", new Date(), source);
    if (diff_day > 60) return gm.date.format(gm.date.parse(source), "yyyy-MM-dd");
    else if (diff_day > 30) return "1个月前";
    else if (diff_day > 14) return "2周前";
    else if (diff_day > 7) return "1周前";
    else if (diff_day > 1) return diff_day + "天前";
    else if (diff_hour > 1) return diff_hour + "小时前";
    else if (diff_minute > 1) return diff_minute + "分钟前";
    else return "刚刚";
};

gm.date.addyear = function (source, yearadd) {
    var pad = gm.number.pad,
     year = source.getFullYear() + yearadd,
     month = source.getMonth(),
     date2 = source.getDate(),
     hours = source.getHours(),
     minutes = source.getMinutes(),
     seconds = source.getSeconds();

    return new Date(year, month, date2, hours, minutes, seconds);
}
gm.date.addday = function (source, dayadd) {
    var pad = gm.number.pad,
     year = source.getFullYear(),
     month = source.getMonth(),
     date2 = source.getDate() + dayadd,
     hours = source.getHours(),
     minutes = source.getMinutes(),
     seconds = source.getSeconds();

    return new Date(year, month, date2, hours, minutes, seconds);
}

//#endregion